package com.example.cryptographic_calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigInteger;

/**
 * @author shouquan
 * date 2021.01
 */
public class MainActivity extends AppCompatActivity {
    TextView editView1;
    TextView editView2;
    TextView textView;
    TextView result;
    Button calculate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editView1 = findViewById(R.id.editText);
        editView2 = findViewById(R.id.editText2);
        result = findViewById(R.id.textView);
        calculate = findViewById(R.id.button);
        textView = findViewById(R.id.textView2);

        calculate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    mod();
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    /**
     * 菜单的点击事件
     */
    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.mod:
                textView.setText("Mod");
                calculate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            mod();
                        } catch (Exception e) {
                            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.gcd:
                textView.setText("Gcd");
                calculate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            gcd();
                        } catch (Exception e) {
                            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case R.id.gcdex:
                textView.setText("GcdEx");
                calculate.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            gcdex();
                        } catch (Exception e) {
                            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            default:
        }
        return true;
    }

    /**
     * 创建菜单
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //通过getMenuInflater()方法得到MenuInflater对象，再调用它的inflate()方法就可以给当前活动创建菜单了，第一个参数：用于指定我们通过哪一个资源文件来创建菜单；第二个参数：用于指定我们的菜单项将添加到哪一个Menu对象当中。
        getMenuInflater().inflate(R.menu.menu, menu);
        // true：允许创建的菜单显示出来，false：创建的菜单将无法显示。
        return true;
    }


    @SuppressLint("SetTextI18n")
    public void mod() {
        String aTemp = editView1.getText().toString();
        String bTemp = editView2.getText().toString();
        if (!aTemp.isEmpty()&& !bTemp.isEmpty()) {
            if (aTemp.contains("^") && !bTemp.contains("^")) {
                //获取数据
                String[] temp = aTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp[0]);
                BigInteger a2 = new BigInteger(temp[1]);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a1.modPow(a2, b);
                //计算结果
                result.setText(resultTemp.toString());
            } else if (!aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                BigInteger a = new BigInteger(aTemp);
                String[] temp = bTemp.split("\\^");
                BigInteger b1 = new BigInteger(temp[0]);
                int b2 = Integer.parseInt(temp[1]);
                BigInteger resultTemp = a.mod(b1.pow(b2));
                //计算结果
                result.setText(resultTemp.toString());
            } else if (aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                String[] temp1 = aTemp.split("\\^");
                String[] temp2 = bTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp1[0]);
                BigInteger a2 = new BigInteger(temp1[1]);
                BigInteger b1 = new BigInteger(temp2[0]);
                int b2 = Integer.parseInt(temp2[0]);
                //计算结果
                BigInteger resultTemp = a1.modPow(a2, b1.pow(b2));
                result.setText(resultTemp.toString());
            } else if (!aTemp.contains("^") && !bTemp.contains("^")) {
                BigInteger a = new BigInteger(aTemp);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a.mod(b);
                result.setText(resultTemp.toString());
            } else {
                Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressLint("SetTextI18n")
    public void gcd() {
        String aTemp = editView1.getText().toString();
        String bTemp = editView2.getText().toString();
        if (!aTemp.isEmpty() && !bTemp.isEmpty()) {
            if (aTemp.contains("^") && !bTemp.contains("^")) {
                //获取数据
                String[] temp = aTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp[0]);
                int a2 = Integer.parseInt(temp[1]);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a1.pow(a2).gcd(b);
                //计算结果
                result.setText(resultTemp.toString());
            }
            else if (!aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                BigInteger a = new BigInteger(aTemp);
                String[] temp = bTemp.split("\\^");
                BigInteger b1 = new BigInteger(temp[0]);
                int b2 = Integer.parseInt(temp[1]);
                BigInteger resultTemp = a.gcd(b1.pow(b2));
                //计算结果
                result.setText(resultTemp.toString());
            } else if (aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                String[] temp1 = aTemp.split("\\^");
                String[] temp2 = bTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp1[0]);
                int a2 = Integer.parseInt(temp1[1]);
                BigInteger b1 = new BigInteger(temp2[0]);
                int b2 = Integer.parseInt(temp2[0]);
                //计算结果
                BigInteger resultTemp = a1.pow(a2).gcd(b1.pow(b2));
                result.setText(resultTemp.toString());
            } else if (!aTemp.contains("^") && !bTemp.contains("^")) {
                BigInteger a = new BigInteger(aTemp);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a.gcd(b);
                result.setText(resultTemp.toString());
            } else {
                Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressLint("SetTextI18n")
    public void gcdex() {
        String aTemp = editView1.getText().toString();
        String bTemp = editView2.getText().toString();
        if (!aTemp.isEmpty() && !bTemp.isEmpty()) {
            if (aTemp.contains("^") && !bTemp.contains("^")) {
                //获取数据
                String[] temp = aTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp[0]);
                int a2 = Integer.parseInt(temp[1]);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a1.pow(a2).modInverse(b);
                //计算结果
                result.setText(resultTemp.toString());
            }
            else if (!aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                BigInteger a = new BigInteger(aTemp);
                String[] temp = bTemp.split("\\^");
                BigInteger b1 = new BigInteger(temp[0]);
                int b2 = Integer.parseInt(temp[1]);
                BigInteger resultTemp = a.modInverse(b1.pow(b2));
                //计算结果
                result.setText(resultTemp.toString());
            } else if (aTemp.contains("^") && bTemp.contains("^")) {
                //获取数据
                String[] temp1 = aTemp.split("\\^");
                String[] temp2 = bTemp.split("\\^");
                BigInteger a1 = new BigInteger(temp1[0]);
                int a2 = Integer.parseInt(temp1[1]);
                BigInteger b1 = new BigInteger(temp2[0]);
                int b2 = Integer.parseInt(temp2[0]);
                //计算结果
                BigInteger resultTemp = a1.pow(a2).modInverse(b1.pow(b2));
                result.setText(resultTemp.toString());
            } else if (!aTemp.contains("^") && !bTemp.contains("^")) {
                BigInteger a = new BigInteger(aTemp);
                BigInteger b = new BigInteger(bTemp);
                BigInteger resultTemp = a.modInverse(b);
                result.setText(resultTemp.toString());
            } else {
                Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(MainActivity.this, "请正确输入", Toast.LENGTH_SHORT).show();
        }
    }


}


